package com.srit.system.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.srit.core.bean.Page;
import com.srit.forecast.constant.Constant;
import com.srit.system.bean.CodeType;
import com.srit.system.bean.GridAttribute;
import com.srit.system.dao.CodeTypeDao;
import com.srit.system.dao.GridAttributeDao;

/**
 * 属性设置
 * 
 * @author guan
 * 
 */
@Controller
public class GridAttributeController {
	protected static Logger log = Logger
			.getLogger(GridAttributeController.class);

	@Autowired(required = true)
	private GridAttributeDao gridAttributeDao;
	@Autowired(required = true)
	private CodeTypeDao codeTypeDao;

	@RequestMapping(value = "/gridAttribute-search")
	public String search(@ModelAttribute Page page,
			@ModelAttribute Object searchForm) {
		gridAttributeDao.page(page);
		return "system/gridAttribute";
	}

	@RequestMapping(value = "/gridAttribute-form")
	public String form(@ModelAttribute GridAttribute gridAttribute,
			ModelMap modelMap) {
		List<String> showTypeList = initSelectList();
		List<CodeType> codeTypeList = codeTypeDao.listAll();
		modelMap.addAttribute("showTypeList", showTypeList);
		modelMap.addAttribute("codeTypeList", codeTypeList);
		return "system/gridAttribute";
	}

	@RequestMapping(value = "/gridAttribute-edit")
	public String edit(@RequestParam int id,
			@ModelAttribute GridAttribute gridAttribute, ModelMap modelMap) {
		if (log.isDebugEnabled()) {
			log.debug(" id is:" + id);
		}
		List<String> showTypeList = initSelectList();
		List<CodeType> codeTypeList = codeTypeDao.listAll();
		modelMap.addAttribute("showTypeList", showTypeList);
		modelMap.addAttribute("codeTypeList", codeTypeList);
		BeanUtils.copyProperties(gridAttributeDao.getById(id), gridAttribute);
		return "system/gridAttribute";
	}

	private List<String> initSelectList() {
		ArrayList<String> showTypeList = new ArrayList<String>();
		showTypeList.add(Constant.GRID_ATTR_TEXT);
		showTypeList.add(Constant.GRID_ATTR_SELECT);
		showTypeList.add(Constant.GRID_ATTR_RADIO);
		showTypeList.add(Constant.GRID_ATTR_TEXTAREA);
		return showTypeList;
	}

	@RequestMapping(value = "/gridAttribute-saveOrUpdate", method = RequestMethod.POST)
	public String saveOrUpdateReportAttribute(GridAttribute gridAttribute) {
		if (log.isDebugEnabled()) {
			log.debug("Request ReportAttribute is:" + gridAttribute);
		}
		if (gridAttribute.getId() != null && gridAttribute.getId() != 0) {
			gridAttributeDao.update(gridAttribute);
		} else {
			gridAttributeDao.save(gridAttribute);
		}
		// foward到查询页面
		return "forward:/gridAttribute-search";
	}

	@RequestMapping(value = "/gridAttribute-del", method = RequestMethod.POST)
	public String deleteReportAttribute() {
		// 传入待删除的id
		return "forward:/gridAttribute-search";
	}

}
